package hk.com.fgoproduction.getdroplets.Lib.TreeList

import hk.com.fgoproduction.getdroplets.Lib.FileMan.FileMan

/**
 * Node of Tree List
 */
class TreeNode() {

    var path = ""        //path
    var name = ""        //display name

    //var id = "" //unique id of node
    var level = 0 //level->parent+1
    var parentPath: String? = null //parent path
    var hasChildren = false //has child?
    var isExpanded = false //expanded?
    var isDirectory = true

    var isEmpty = true

    companion object {
        val LEVEL_TOP: Int = 0
        val PATH_TOP: String = FileMan.ROOT
        val NAME_TOP: String = PATH_TOP
    }

    private operator fun invoke(path: String,
                                name: String,
                                level: Int,
                                parentPath: String?,
                                hasChildren: Boolean,
                                isExpanded: Boolean,
                                isDirectory: Boolean) {
        this.path = path
        this.name = name
        this.level = level
        this.parentPath = parentPath
        this.hasChildren = hasChildren
        this.isExpanded = isExpanded
        this.isDirectory = isDirectory
        isEmpty = false
    }

    constructor(path: String,
                name: String,
                level: Int,
                parentPath: String?,
                isDirectory: Boolean = true,
                hasChildren: Boolean = false,
                isExpanded: Boolean = false) : this() {
        this(path, name, level, parentPath, hasChildren, isExpanded, isDirectory)
    }

    constructor(parent: TreeNode, path: String, name: String, isDirectory: Boolean, hasChildren: Boolean = false, isExpanded: Boolean = false) : this() {
        this(path, name, parent.level + 1, parent.path, hasChildren, isExpanded, isDirectory)
    }

    override fun equals(other: Any?): Boolean {
        if (other !is TreeNode)
            return false
        else if (other.isEmpty)
            return false
        else if (other.path !== path)
            return false
        else
            return true
    }

    override fun hashCode(): Int {
        return path.hashCode()
    }

    override fun toString(): String {
        return path + "@Level$level"
    }

    class NullTreeNodeException : Exception("Null Node!")
}